<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/19
 * Time: 15:34
 */

namespace Admin\Controller;


use Think\Controller;

class ExcelController extends Controller
{
    //导出表格
    public function excel_out(){

        vendor('PHPExcel.PHPExcel');
        $objExcel = new \PHPExcel();

        //使用Excel5对应的类,生成.xls文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');

        //设置水平垂直居中
        $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        //设置字体和样式
        $objExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);

        $objExcel->getActiveSheet()->getStyle('A2:D2')->getFont()->setBold(true);

        $objExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);

        //设置第一行、第二行的默认高度
        $objExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);

        $objExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);

        $objExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(20);

        $objExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(20);

        $objExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(20);

        //设置某一列的宽度
        $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);

        $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);

        $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);

        $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);

        $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);

        $objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);

        $objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);

        $type = S('type');
        $data = S('list_wages');

        $count=count($data);
        if($type==1){
            //设置表头(第一行合并)：
            $objExcel->getActiveSheet()->mergeCells('A1:G1');

            $objActSheet = $objExcel->getActiveSheet(0);

            //填写表头内容：
            $objActSheet ->setCellValue('A1','工资汇总列表');

            $objActSheet ->setCellValue('A2','员工编号');

            $objActSheet ->setCellValue('B2','门店编号');

            $objActSheet ->setCellValue('C2','门店名称');

            $objActSheet ->setCellValue('D2','员工手机');

            $objActSheet ->setCellValue('E2','提成金额');

            $objActSheet ->setCellValue('F2','提成笔数');

            $objActSheet ->setCellValue('G2','工资总计');

            $baseRow = 3; //前面设置了第一行是合并后的标题栏，第二行为表的各个字段名，所以数据是从第三行开始导入

            //将二维数组循环输出
            foreach ( $data as $key => $value ) {

                $i = $baseRow + $key;

                $objExcel->getActiveSheet()->setCellValue('A'.$i,$value['bind_id']);

                $objExcel->getActiveSheet()->setCellValue('B'.$i,$value['bind_did']);

                $objExcel->getActiveSheet()->setCellValue('C'.$i,$value['bind_name']);

                $objExcel->getActiveSheet()->setCellValue('D'.$i,$value['tel']);

                $objExcel->getActiveSheet()->setCellValue('E'.$i,$value['zmoney']);

                $objExcel->getActiveSheet()->setCellValue('F'.$i,$value['count']);

                $objExcel->getActiveSheet()->setCellValue('G'.$i,$value['wages']);

            }
        }else{
            //设置表头(第一行合并)：
            $objExcel->getActiveSheet()->mergeCells('A1:F1');

            $objActSheet = $objExcel->getActiveSheet(0);
            //填写表头内容：
            $objActSheet ->setCellValue('A1','充值奖金汇总列表');

            $objActSheet ->setCellValue('A2','员工编号');

            $objActSheet ->setCellValue('B2','门店编号');

            $objActSheet ->setCellValue('C2','门店名称');

            $objActSheet ->setCellValue('D2','员工手机');

            $objActSheet ->setCellValue('E2','提成金额');

            $objActSheet ->setCellValue('F2','提成笔数');

            $baseRow = 3; //前面设置了第一行是合并后的标题栏，第二行为表的各个字段名，所以数据是从第三行开始导入

            //将二维数组循环输出
            foreach ( $data as $key => $value ) {

                $i = $baseRow + $key;

                $objExcel->getActiveSheet()->setCellValue('A'.$i,$value['bind_id']);

                $objExcel->getActiveSheet()->setCellValue('B'.$i,$value['bind_did']);

                $objExcel->getActiveSheet()->setCellValue('C'.$i,$value['bind_name']);

                $objExcel->getActiveSheet()->setCellValue('D'.$i,$value['tel']);

                $objExcel->getActiveSheet()->setCellValue('E'.$i,$value['zmoney']);

                $objExcel->getActiveSheet()->setCellValue('F'.$i,$value['count']);

            }
        }


        $objExcel->setActiveSheetIndex(0);
        ob_end_clean() ;
        header('Content-Type: applicationnd.ms-excel');
        header('Content-Disposition: attachment;filename="汇总列表('.date('Ymd-His').').xls"');

        header('Cache-Control: max-age=0');
        //支持浏览器下载生成的文档
        S('list_wages',null);
        S('type',null);
        $objWriter->save('php://output');

    }

}